package com.jingdianjichi.oss.adapter;

import com.jingdianjichi.oss.entity.FileInfo;
import com.jingdianjichi.oss.adapter.StorageAdapter;
import com.jingdianjichi.oss.util.MinioUtil;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;


public class MinioStorageAdapter implements StorageAdapter {
    @Resource
    MinioUtil minioUtil;

    @Override
    public void createBucket(String bucket) throws Exception {
        minioUtil.createBucket(bucket);
    }


    @Override
    public void uploadFile(MultipartFile uploadFile, String bucket, String objectName) throws Exception {

        minioUtil.createBucket(bucket);
        if (objectName != null) {
            minioUtil.uploadFile(uploadFile.getInputStream(), bucket, objectName + uploadFile.getName());
        } else {
            minioUtil.uploadFile(uploadFile.getInputStream(), bucket, uploadFile.getName());
        }
    }

    @Override
    /**
     *     @SneakyThrows lombok依赖的一个注解，可以帮助抛异常
     */

    public List<String> getAllBucket() throws Exception {


        return minioUtil.getAllBucket();
    }

    @Override
    public List<FileInfo> getAllFiles(String bucket) throws Exception {
        return minioUtil.getAllFiles(bucket);
    }

    @Override
    public InputStream download(String bucket, String objectName) throws Exception {
        return minioUtil.download(bucket, objectName);
    }

    @Override
    public void deleteBuckets(String bucket) throws Exception {

        minioUtil.deleteBuckets(bucket);
    }

    @Override
    public void deleteFiles(String bucket, String objectName) throws Exception {
        minioUtil.deleteFiles(bucket, objectName);
    }
}

